(function($){
	$.fn.userInit = function(){
		var $this = $(this);
		var id = $(this).attr('id');
		var selectRow = null;
		var selectIndex = null;
		
		var editDialog= $('#editDialog',$this);
		
		$('#shopID',editDialog).combobox({
			data:TEA.getShopList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:250
		})
		//添加
		var onAdd = function(){
			$('input[name="roleType"]:first',editDialog).attr('checked',true);
			$(editDialog).dialog('open');
		} 
		//修改
		var onUpdate = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$(editDialog).dialog('open');
			$('#userForm',editDialog).form('load',selectRow);
		}
		//刷新列表
		var onReload = function(){
			$('#usertList',$this).datagrid('reload');
		}
		//加载列表
		$('#usertList',$this).datagrid({
			url:"user!select.action",
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			toolbar:[	
						{id:'add',text:'添加',iconCls:'icon-add',handler:function(){onAdd()}},
						{id:'update',text:'修改',iconCls:'icon-edit',handler:function(){onUpdate()}},
						{id:'reload',text:'刷新',iconCls:'icon-reload',handler:function(){onReload()}}
					],
			columns:[[
				{field:'userID',hidden:true},
				{field:'shopID',hidden:true},
				{field:'userCode',title:'用户编号',width:150,align:"center"},
				{field:'userName',title:'用户名称',width:200,align:"center"},
				{field:'roleType',title:'角色',width:100,align:"center",
					formatter: function(value,row,index){
						if (value=='0'){
							return '店员';
						} else if(value=='1'){
							return '店长';
						}
						return '老板';
					}
				},
				{field:'shopName',title:'所属店面',width:200,align:"center"},
			]],
			onClickRow:function(rowIndex, rowData){
				selectRow = rowData;
				selectIndex = rowIndex;
			},
			onDblClickRow:function(rowIndex,rowData){
				onUpdate();
			},
			onLoadSuccess:function(){
				selectRow = null;
		 		selectIndex = null;
			}
		});
		
		var onSave = function(){
			var userCode = $('#userCode',editDialog).val();
			if($.trim(userCode)==''){
				$.messager.alert('提示','用户编号不能为空','warning');
				return;
			}
			var userName = $('#userName',editDialog).val();
			if($.trim(userName)==''){
				$.messager.alert('提示','用户名称不能为空','warning');
				return;
			}
			var roleType = $('input[name="roleType"]:checked',editDialog).val();
			
			if($.trim(roleType)==''){
				$.messager.alert('提示','角色不能为空','warning');
				return;
			}
			var shopID = $('#shopID',editDialog).combobox('getValue');
			if(shopID==null||$.trim(shopID)==''){
				$.messager.alert('提示','所属店面不能为空','warning');
				return;
			}
			var url = null;
			if($('#userID',editDialog).val()!=''){
				url= 'user!update.action';
			}else{
				url= 'user!add.action';
			}
			$('#userForm',editDialog).ajaxSubmit({
				url:url,
				type:'post',
				dataType:'json',
				success:function(result){	
					if(result.success){
						var fn = function(){
							onReload();
							$(editDialog).dialog('close');
							$('#userForm',editDialog).form('clear');
						}
						$.messager.alert('提示','保存成功','info',fn);
					}else{
						$.messager.alert('提示',result.msg,"warning");
					}
				}
			});
		}
		
		var onExit = function(){
			$(editDialog).dialog('close');
			$('#userForm',editDialog).form('clear');
		}
		
		$(editDialog).dialog({  
		    title: '编辑基础数据',  
		    width:600,
		    height:400,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}
			}]
		});
	}
})(jQuery);